package io.renren.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.renren.bean.ExamInfo;
import io.renren.dao.ExamTemplateDao;
import io.renren.entity.ExamTemplateEntity;
import io.renren.service.ExamTemplateService;
import org.springframework.stereotype.Service;

import java.util.Map;


@Service("examTemplateService")
public class ExamTemplateServiceImpl extends ServiceImpl<ExamTemplateDao, ExamTemplateEntity> implements ExamTemplateService {

    /**
     * 获取考试信息
     *
     * @param studentId 学员id
     * @param subjectId 科目id
     * @return R
     * @author huolikang
     * @date 2018年10月30日
     */
    @Override
    public ExamInfo getExamInfo(Long studentId, Integer subjectId) {
        ExamInfo examInfo = new ExamInfo();
        //获取考试信息
        Map<String, Object> map = baseMapper.getExamInfo(subjectId);
        if (map.size() > 0 && map != null) {
            // 试卷id
            Integer examPaperId = map.get("examPaperId") == null ? null : Integer.parseInt(map.get("examPaperId").toString());
            // 试卷名称
            String examName = map.get("examName") == null ? "" : map.get("examName").toString();
            // 注意事项
            String mattersNeedAttention = map.get("mattersNeedAttention") == null ? "" : map.get("mattersNeedAttention").toString();
            // 考试规格
            String examSpec = map.get("examSpec") == null ? "" : map.get("examSpec").toString();
            examInfo.setExamName(examName);
            examInfo.setExamSpec(examSpec);
            examInfo.setMattersNeedAttention(mattersNeedAttention);
            if (examPaperId != null) {
                //获取学员的考试次数，剩余次数
                Map<String, Object> scoreMap = baseMapper.getScoreMap(studentId, subjectId);
                if (scoreMap != null && scoreMap.size() > 0) {
                    // 平均分
                    String avgScore = scoreMap.get("avgScore") == null ? null : scoreMap.get("avgScore").toString();
                    // 已考次数
                    Integer examFrequency = scoreMap.get("examFrequency") == null ?
                            0 : Double.valueOf(scoreMap.get("examFrequency").toString()).intValue();
                    // 剩余次数
                    String remainFrequency = "不限";
                    // 科目考试有效次数
                    Integer validFrequency = scoreMap.get("validFrequency") == null ? 0
                            : Double.valueOf(scoreMap.get("validFrequency").toString()).intValue();
                    // 剩下次数
                    if (validFrequency != 0) {
                        remainFrequency = String.valueOf(validFrequency - examFrequency);
                    }
                    // 平均分
                    examInfo.setAvgScore(avgScore);
                    // 剩下次数
                    examInfo.setRemainFrequency(remainFrequency);
                }
            }

        }
        return examInfo;
    }
}
